<?php
namespace Home\Controller;
use Think\Controller;
class PrivController extends CommonController{
	public function index(){
		$sql = "select p.*, pl.leve from oa_sys_priv p, oa_sys_priv_leve pl where p.priv_leve=pl.id order by p.priv_no";
		$data = M()->query($sql);
		$this->assign('data',$data);
		$this->display();
//		$this->redirect("purview");
	}
	
	public function add(){
		$leve = M("sys_priv_leve");
		$data_leve = $leve->select();
		$this->assign("data_leve", $data_leve);
		$this->display();
	}
	
	public function insert(){
		$data = $_POST;
		$priv = M("sys_priv");
		$success = $priv->data($data)->add();
		if($success){
			$this->systemlog("add", "角色管理", session('name')."添加角色".$data['priv_name']);
			$this->success("添加角色名成功！","index");
		}else{
			$this->error("添加角色名失败！");
		}
	}
	
	public function edit(){
		$id = I('get.id');
		$priv = M("sys_priv");
		$leve = M("sys_priv_leve");
		$data = $priv->where("id=%d",$id)->find();
		$data_leve = $leve->select();
		$this->assign("data", $data);
		$this->assign("data_leve", $data_leve);
		$this->display();
	}
	
	public function save(){
		$data = $_POST;
		$id = I('post.id');
		$priv = M("sys_priv");
		$success = $priv->where("id=%d",$id)->save($data);
		if($success){
			$this->systemlog("edit", "角色管理", session('name')."修改角色".$data['priv_name']);
			$this->success("修改角色名成功！","index");
		}else{
			$this->error("修改角色名失败！");
		}
	}
	
	public function delete(){
		$id = I('get.id');
		$priv = M("sys_priv");
		$priv_name = $priv->where("id=%d",$id)->getField("priv_name");
		$success = $priv->where("id=%d",$id)->delete();
		if($success){
			$this->systemlog("delete", "角色管理", session('name')."删除角色".$priv_name);
			$this->success("删除角色名成功！","index");
		}else{
			$this->error("删除角色名失败！");
		}
	}
	
	public function purview(){
		$id = I('get.id');
		if(!is_id($id)){$this->error("非法参数！");}
		$priv = M("sys_priv");
		$menu = M("sys_menu");
		$data_priv = $priv->where('id=%d',$id)->find();
		$purview = explode(",", $data_priv['purview']);
		$data_menu = $menu->where('leve=0 and status=1')->order('sort asc')->select();//一级菜单
		foreach ($data_menu as $key=>$value){
			$id = $value['menu_id'];
			$data_menu[$key]['one'] = $menu->where('leve=%d and pid=0 and status=1',$id)->order('sort asc')->select();//二级菜单
			for ($i=0;$i<count($data_menu[$key]['one']);$i++){
				$id2 = $data_menu[$key]['one'][$i]['menu_id'];
				$data_menu[$key]['one'][$i]['second'] = $menu->where('leve=%d and pid=%d',$id,$id2)->order('sort asc')->select();//三级菜单
			}
		}
		$this->assign('purview',$purview);
		$this->assign('priv_name',$data_priv['priv_name']);
		$this->assign('priv_id',$data_priv['id']);
		$this->assign('data_menu',$data_menu);
		$this->display();
	}
	
	public function purview_save(){
		if(!IS_POST){$this->error("非法表单！");}
		$priv_id = I('post.priv_id');
		if(!is_id($priv_id)){$this->error("非法参数！");}
		$purview = $_POST['key'];
		$purview = implode(",", $purview);
		$priv = M("sys_priv");
		$priv_name = $priv->where('id=%d',$priv_id)->getField("priv_name");
		$success = $priv->where('id=%d',$priv_id)->setField("purview", $purview);
		if($success)
		{
			$this->systemlog('edit', '角色管理', '修改角色---'.$priv_name.'--的权限');
			$this->success('修改权限成功！','index');
		}else{
			$this->error('修改权限失败！');
		}
	}
    
    public function _empty($name){
    	$this->emptyaction($name);
    }
}
?>